package com.gexiaobao.pigeon.ui.fragment.match

import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseFragment
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.ext.initClose
import com.gexiaobao.pigeon.app.ext.loadListData
import com.gexiaobao.pigeon.app.ext.loadServiceInit
import com.gexiaobao.pigeon.app.ext.showLoading
import com.gexiaobao.pigeon.app.model.bean.TraPassListResponse
import com.gexiaobao.pigeon.app.util.Util
import com.gexiaobao.pigeon.app.weight.recycleview.SpaceItemDecoration
import com.gexiaobao.pigeon.databinding.FragmentTrainingAndReleaseBinding
import com.gexiaobao.pigeon.ui.adapter.TraPassListAdapter
import com.gexiaobao.pigeon.ui.race.RaceViewModel
import com.kingja.loadsir.core.LoadService
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.navigateAction

/**
 * @Author      : hxw
 * @Date        : 2023/6/1 10:45
 * @Describe    : 我的比赛-训放赛
 */
class FragmentTrainingAndRelease : BaseFragment<RaceViewModel, FragmentTrainingAndReleaseBinding>() {

    private var mRaceId = 0
    private var mFollowStatus = 0
    private val mAdapter: TraPassListAdapter by lazy { TraPassListAdapter(arrayListOf()) }
    private var mResultData: ArrayList<TraPassListResponse.DataList> = arrayListOf()

    //界面状态管理者
    private lateinit var loadsir: LoadService<Any>

    override fun initView(savedInstanceState: Bundle?) {
        mRaceId = requireArguments().getInt("raceId")
        mFollowStatus = requireArguments().getInt("followStatus")
        mDatabind.includeBar.toolbar.initClose("训放赛") {
            nav().navigateUp()
        }
    }

    override fun initData() {
        super.initData()
        mDatabind.layoutList.swipeRecyclerView.init(LinearLayoutManager(context), mAdapter).addItemDecoration(
            SpaceItemDecoration(0, Util.dp2px(context, 5f))
        )
        //状态页配置
        loadsir = loadServiceInit(mDatabind.layoutList.swipeRefreshLayout) {
            //点击重试时触发的操作
            loadsir.showLoading()
            mViewModel.getTraPassList(mRaceId)
        }

        mDatabind.layoutList.swipeRefreshLayout.init {
            loadsir.showLoading()
            mViewModel.getTraPassList(mRaceId)
        }

        mAdapter.run {
            setOnItemClickListener { _, _, position ->
                nav().navigateAction(R.id.action_to_training_info, Bundle().apply {
                    putString("title", mAdapter.data[position].trainingDis)
                    putInt("id", mAdapter.data[position].passId)
                    putInt("raceId", mRaceId)
                    putInt("passStatus", mAdapter.data[position].passStatus)
                    putInt("followStatus", mFollowStatus)
                })
            }
        }
    }

    override fun lazyLoadData() {
        super.lazyLoadData()
        loadsir.showLoading()
        mViewModel.getTraPassList(mRaceId)
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.traPassResult.observe(viewLifecycleOwner) {
            if (it.listData != null) {
                mResultData.addAll(it.listData)
            }
            loadListData(it, mAdapter, loadsir, mDatabind.layoutList.swipeRecyclerView, mDatabind.layoutList.swipeRefreshLayout)
        }
    }
}